package com.bjpowernode.service.impl;

import com.bjpowernode.POJO.ProductInfo;
import com.bjpowernode.POJO.ProductInfoExample;
import com.bjpowernode.mapper.ProductInfoMapper;
import com.bjpowernode.service.ProductInfoService;
import com.bjpowernode.service.request.PageAndPNameAndTypeIdAndpPriceLikeQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductInforServiceImpl implements ProductInfoService {
    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> getAll() {
        List<ProductInfo> productInfos = productInfoMapper.selectByExample(null);

        return productInfos;
    }

    @Override
    public PageInfo<ProductInfo> splitPage(Integer currentPage, int pageSize) {

        // 分页插件使用PageHelper 工具类完成设置
        PageHelper.startPage(currentPage,pageSize);

        // 进行PageInfo 的数据封装
        // 进行有条件的查询操作，必须要创建ProductInfoExample 对象
        ProductInfoExample productInfoExample = new ProductInfoExample();
        // 设置排序，按主键排序
        productInfoExample.setOrderByClause("p_id desc");
        List<ProductInfo> list = productInfoMapper.selectByExample(productInfoExample);

        // 将查询到的集合封装进PageInfo对象中
        PageInfo<ProductInfo> productInfoPageInfo = new PageInfo<>(list);

        // 删除bug 的修复
        if(currentPage > productInfoPageInfo.getPages()){
            PageHelper.startPage(currentPage-1,pageSize);

            // 进行PageInfo 的数据封装
            // 进行有条件的查询操作，必须要创建ProductInfoExample 对象
            productInfoExample = new ProductInfoExample();
            // 设置排序，按主键排序
            productInfoExample.setOrderByClause("p_id desc");
            list = productInfoMapper.selectByExample(productInfoExample);
            productInfoPageInfo = new PageInfo<>(list);
            return productInfoPageInfo;
        }


        return productInfoPageInfo;
    }

    @Override
    public int save(ProductInfo info) {
        int num = productInfoMapper.insert(info);
        return num;
    }

    @Override
    public ProductInfo getById(Integer id) {
        return productInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<ProductInfo> selectByCondition(PageAndPNameAndTypeIdAndpPriceLikeQuery likeQuery, int pageSize) {
        PageHelper.startPage(likeQuery.getPage(),pageSize);
        List<ProductInfo> productInfoList = productInfoMapper.selectByCondition(likeQuery);
        PageInfo<ProductInfo> productInfoPageInfo = new PageInfo<>(productInfoList);
        return productInfoPageInfo;
    }

    @Override
    public int update(ProductInfo info) {
        return productInfoMapper.updateByPrimaryKeySelective(info);
    }

    @Override
    public int deleteById(Integer pid) {
        return productInfoMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public int deleteBatch(int[] pids) {
        int influenceRow = 0;
        for (int pid : pids) {
            influenceRow += deleteById(pid);
        }

        return influenceRow;
    }
}
